import { defineStore } from "pinia";
import { ElNotification } from "element-plus";
import axios from "axios";
import dayjs from "dayjs";
import pinia from "@/store/index.js";
import useActivityStore from "@/store/activity.js";
import useCommonStore from "@/store/common.js";

const activityStore = useActivityStore(pinia);
const commonStore = useCommonStore(pinia);

const useStore = defineStore({
  id: "runway",
  state: () => ({
    codeZUUU: "",
    codeZUTF: "",
    version: 0,
    time: "",
  }),
  getters: {
    isActiveWJ() {
      if (!activityStore || !activityStore.militaryStatus) {
        return false;
      } else {
        return (
          activityStore.militaryStatus.filter((cell) =>
            (cell.arrField + cell.depField + cell.unit).includes("温江")
          ).length > 0
        );
      }
    },
    utilization() {
      let direction = "";
      let modeCode = "";
      let ZUUUDirection = "N";
      let ZUTFDirection = "N";
      let ZUUU02L = new Array();
      let ZUUU02R = new Array();
      let ZUUU20R = new Array();
      let ZUUU20L = new Array();
      let ZUTF01 = new Array();
      let ZUTF02 = new Array();
      let ZUTF19 = new Array();
      let ZUTF20 = new Array();
      let ZUTF11 = new Array();
      let ZUUUDEP = new Array();
      let ZUUUARR = new Array();
      let ZUTFDEP = new Array();
      let ZUTFARR = new Array();
      if (this.codeZUUU.length == 3) {
        ZUUUDirection = this.codeZUUU.substring(0, 1);
        let westCodeZUUU = this.codeZUUU.substring(1, 2);
        let eastCodeZUUU = this.codeZUUU.substring(2, 3);
        if (ZUUUDirection == "N") {
          switch (westCodeZUUU) {
            case "1":
              ZUUU02L.push("D");
              ZUUUDEP.push("02L");
              break;
            case "2":
              ZUUU02L.push("A");
              ZUUUARR.push("02L");
              break;
            case "3":
              ZUUU02L.push("D");
              ZUUU02L.push("A");
              ZUUUDEP.push("02L");
              ZUUUARR.push("02L");
            default:
              break;
          }
          switch (eastCodeZUUU) {
            case "1":
              ZUUU02R.push("D");
              ZUUUDEP.push("02R");
              break;
            case "2":
              ZUUU02R.push("A");
              ZUUUARR.push("02R");
              break;
            case "3":
              ZUUU02R.push("D");
              ZUUU02R.push("A");
              ZUUUDEP.push("02R");
              ZUUUARR.push("02R");
            default:
              break;
          }
        } else if (ZUUUDirection == "S") {
          switch (westCodeZUUU) {
            case "1":
              ZUUU20R.push("D");
              ZUUUDEP.push("20R");
              break;
            case "2":
              ZUUU20R.push("A");
              ZUUUARR.push("20R");
              break;
            case "3":
              ZUUU20R.push("D");
              ZUUU20R.push("A");
              ZUUUDEP.push("20R");
              ZUUUARR.push("20R");
            default:
              break;
          }
          switch (eastCodeZUUU) {
            case "1":
              ZUUU20L.push("D");
              ZUUUDEP.push("20L");
              break;
            case "2":
              ZUUU20L.push("A");
              ZUUUARR.push("20L");
              break;
            case "3":
              ZUUU20L.push("D");
              ZUUU20L.push("A");
              ZUUUDEP.push("20L");
              ZUUUARR.push("20L");
            default:
              break;
          }
        }
      }
      if (this.codeZUUU.length == 4) {
        ZUTFDirection = this.codeZUTF.substring(0, 1);
        let westCodeZUTF = this.codeZUUU.substring(1, 2);
        let eastCodeZUTF = this.codeZUUU.substring(2, 3);
        let northCodeZUTF = this.codeZUUU.substring(3, 4);
        if (ZUTFDirection == "N") {
          switch (westCodeZUTF) {
            case "1":
              ZUTF01.push("D");
              ZUTFDEP.push("01");
              break;
            case "2":
              ZUTF01.push("A");
              ZUTFARR.push("01");
              break;
            case "3":
              ZUTF01.push("D");
              ZUTF01.push("A");
              ZUTFDEP.push("01");
              ZUTFARR.push("01");
            default:
              break;
          }
          switch (eastCodeZUTF) {
            case "1":
              ZUTF02.push("D");
              ZUTFDEP.push("02");
              break;
            case "2":
              ZUTF02.push("A");
              ZUTFARR.push("02");
              break;
            case "3":
              ZUTF02.push("D");
              ZUTF02.push("A");
              ZUTFDEP.push("02");
              ZUTFARR.push("02");
            default:
              break;
          }
          switch (northCodeZUTF) {
            case "1":
              ZUTF11.push("D");
              ZUTFDEP.push("11");
              break;
            default:
              break;
          }
        } else if (ZUUUDirection == "S") {
          switch (westCodeZUTF) {
            case "1":
              ZUTF19.push("D");
              ZUTFDEP.push("19");
              break;
            case "2":
              ZUTF19.push("A");
              ZUTFARR.push("19");
              break;
            case "3":
              ZUTF19.push("D");
              ZUTF19.push("A");
              ZUTFDEP.push("19");
              ZUTFARR.push("19");
            default:
              break;
          }
          switch (eastCodeZUTF) {
            case "1":
              ZUTF20.push("D");
              ZUTFDEP.push("20");
              break;
            case "2":
              ZUTF20.push("A");
              ZUTFARR.push("20");
              break;
            case "3":
              ZUTF20.push("D");
              ZUTF20.push("A");
              ZUTFDEP.push("20");
              ZUTFARR.push("20");
            default:
              break;
          }
          switch (northCodeZUTF) {
            case "1":
              ZUTF11.push("D");
              ZUTFDEP.push("11");
              break;
            default:
              break;
          }
        }
      }
      direction = ZUUUDirection + ZUTFDirection;
      modeCode = direction + (isActiveWJ ? "WJ" : "");
      return {
        direction,
        modeCode,
        ZUUUDirection,
        ZUTFDirection,
        ZUUU02L,
        ZUUU02R,
        ZUUU20R,
        ZUUU20L,
        ZUTF01,
        ZUTF02,
        ZUTF19,
        ZUTF20,
        ZUTF11,
        ZUUUDEP,
        ZUUUARR,
        ZUTFDEP,
        ZUTFARR,
      };
    },
    direction() {
      return this.utilization.direction;
    },
    modeCode() {
      return this.utilization.modeCode;
    },
  },
  actions: {
    getRunwayStatus() {
      return new Promise((resolve, reject) => {
        axios
          .get("api/seat_runway_get.php")
          .then((response) => {
            this.codeZUUU = response.data.zuuu;
            this.codeZUTF = response.data.zutf;
            this.version = commonStore.isReal
              ? response.data.version
              : new Date().getTime();
            this.time = response.data.time;
            if (commonStore.isReal) {
              if (dayjs().diff(dayjs(this.version), "minute") > 3) {
                ElNotification({ type: "error", title: "跑道信息已过期!" });
              }
            }
            resolve(response.data);
          })
          .catch((error) => {
            console.log;
            reject(error);
          });
      });
    },
  },
});

export default useStore;
